package com.egg.ylt.heartbeat;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.CaloriesEntity;
import com.egg.ylt.pojo.RealTimeHealthyDataEntity;
import com.egg.ylt.pojo.RealtimeDataEntity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

import okhttp3.Call;

/**
 * author:        yupeng
 * created on:    2019/2/27 18:40
 * description:   心跳检测模块(用于在心跳激发时, 轮询服务器请求数据)
 */
public enum HeartbeatManageSingleton {
    getInstance;

    private final int SWIMRING = 100;
    private final int STEELYARD = 101;
    private final int AIRWATER = 102;
    private static final int THREE_SECONDS_IN_MILLISECONDS = 2 * 1000;

    private OnHandleSwimringEventCallback swimringCallback;
    private OnHandleSteelyardEventCallback steelyardCallback;
    private OnHandleAirWaterEventCallback airWaterCallback;
    private Call swimringRequestCall;
    private Call steelyardRequestCall;
    private Call airWaterRequestCall;
    private boolean isStartSwimring = false;
    private boolean isStartSteelyard = false;
    private boolean isStartAirWater = false;
    // 泳圈code
    private String code;
    // 门店id
    private String shopId;

    private Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SWIMRING:
                    handleSwimringEvent();
                    break;

                case STEELYARD:
                    handleSteelyardEvent();
                    break;

                case AIRWATER:
                    handleAirWaterEvent();
                    break;
            }

            handler.sendEmptyMessageDelayed(msg.what, THREE_SECONDS_IN_MILLISECONDS);
        }
    };

    /**
     * 游泳圈
     */
    private void handleSwimringEvent() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("babyId", Constants.BABYID);
        hashMap.put("code", code);
        swimringRequestCall = RequestManager.getInstance().requestPostByAsyn(API.POST_CALORIES, hashMap, new ReqCallBack<CaloriesEntity>() {
            @Override
            public void onReqSuccess(CaloriesEntity result) {
                swimringCallback.onHandleSwimringEvent(result);
            }

            @Override
            public void onFailure(String result) {

            }

            @Override
            public void onReqFailed(ErrorBean error) {

            }
        });
    }

    /**
     * 抚触台
     */
    private void handleSteelyardEvent() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("babyId", Constants.BABYID);
        steelyardRequestCall = RequestManager.getInstance().requestGetByAsyn(API.GET_REALTIME_HEALTHY_DATA, hashMap, new ReqCallBack<RealTimeHealthyDataEntity>() {
            @Override
            public void onReqSuccess(RealTimeHealthyDataEntity result) {
                steelyardCallback.onHandleSteelyardEvent(result);
            }

            @Override
            public void onFailure(String result) {

            }

            @Override
            public void onReqFailed(ErrorBean error) {

            }
        });
    }

    /**
     * 空气水质量
     */
    private void handleAirWaterEvent() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("shopId", shopId);
        airWaterRequestCall = RequestManager.getInstance().requestPostByAsyn(API.POST_REALTIME_DATA, hashMap, new ReqCallBack<RealtimeDataEntity>() {

            @Override
            public void onReqSuccess(RealtimeDataEntity result) {
                airWaterCallback.onHandleAirWaterEvent(result);
            }

            @Override
            public void onFailure(String result) {
            }

            @Override
            public void onReqFailed(ErrorBean error) {
            }
        });
    }

    /**
     * 启动 游泳圈"心跳检测"
     */
    public void startSwimringHeartbeat(OnHandleSwimringEventCallback callback, String code) throws Exception {
        if (callback == null || TextUtils.isEmpty(code)) {
            throw new Exception("callback | code不能为空！");
        }
        if (isStartSwimring) {
            return;
        }
        isStartSwimring = true;
        this.swimringCallback = callback;
        this.code = code;
        handler.sendEmptyMessage(SWIMRING);
    }

    /**
     * 停止 游泳圈"心跳检测"
     */
    public void stopSwimringHeartbeat() {
        handler.removeMessages(SWIMRING);
        if (swimringRequestCall != null) {
            swimringRequestCall.cancel();
        }
        isStartSwimring = false;
    }

    /**
     * 启动 抚触台"心跳检测"
     */
    public void startSteelyardHeartbeat(OnHandleSteelyardEventCallback callback) throws Exception {
        if (callback == null) {
            throw new Exception("callback不能为空！");
        }
        if (isStartSteelyard) {
            return;
        }
        isStartSteelyard = true;
        this.steelyardCallback = callback;
        handler.sendEmptyMessage(STEELYARD);
    }

    /**
     * 停止 抚触台"心跳检测"
     */
    public void stopSteelyardHeartbeat() {
        handler.removeMessages(STEELYARD);
        if (steelyardRequestCall != null) {
            steelyardRequestCall.cancel();
        }
        isStartSteelyard = false;
    }

    /**
     * 启动 空气水质量"心跳检测"
     * @param callback
     * @param shopId
     * @throws Exception
     */
    public void startAirWaterHeartbeat(OnHandleAirWaterEventCallback callback, String shopId) throws Exception {
        if (callback == null || TextUtils.isEmpty(shopId)) {
            throw new Exception("callback | shopId不能为空！");
        }
        if (isStartAirWater) {
            return;
        }
        this.airWaterCallback = callback;
        this.shopId = shopId;
        handler.sendEmptyMessage(AIRWATER);
    }

    /**
     * 停止 空气水质量"心跳检测"
     */
    public void stopAirWaterHeartbeat() {
        handler.removeMessages(AIRWATER);
        if (airWaterRequestCall != null) {
            airWaterRequestCall.cancel();
        }
        isStartAirWater = false;
    }

    /**
     * 关闭 所有"心跳检测"
     */
    public void stopAllHeartbeat() {
        stopAirWaterHeartbeat();
        stopSteelyardHeartbeat();
        stopSwimringHeartbeat();
    }

    /**
     * 游泳圈事件回调
     */
    public interface OnHandleSwimringEventCallback {
        void onHandleSwimringEvent(CaloriesEntity entity);
    }

    /**
     * 抚触台事件回调
     */
    public interface OnHandleSteelyardEventCallback {
        void onHandleSteelyardEvent(RealTimeHealthyDataEntity entity);
    }

    /**
     * 空气水质量事件回调
     */
    public interface OnHandleAirWaterEventCallback {
        void onHandleAirWaterEvent(RealtimeDataEntity entity);
    }

}
